package shemes;

import java.awt.Point;
import java.util.ArrayList;

import gui.*;
import components.*;

public class Mem2 {
    
    private static OR OR1;
    private static NOT NOT1;
    private static AND AND1, AND2;
    private static REG MEMACC;
    private static REG TIME;
    private static CMP CMP1;
    private static BoolsToInt zeroes;
    private static GuiScheme gui;
    
    public static GuiScheme getGui() {
        return gui;
    }
    
    public static void init() {
        OR1 = new OR();
        NOT1 = new NOT();
        AND1 = new AND();
        AND2 = new AND();
        
        MEMACC = new REG(8, "MEMACC");// BOOLS
        MEMACC.setClock(1);
        MEMACC.getOut(0).setIsInt();
        MEMACC.getOut(0).setNumOfLines(8);
        TIME = new REG(8, "TIME");
        TIME.setClock(1);
        TIME.getOut(0).setIsInt();
        TIME.getOut(0).setNumOfLines(8);
        
        CMP1 = new CMP(1);
        
        zeroes = new BoolsToInt(8, 8);
        
        
    }
    
    public static void connect() {
        OR1.setInputPin(0, Mem1.rdMEM());
        OR1.setInputPin(1, Mem1.wrMEM());
        NOT1.setInputPin(0, Mem2.fcMEM());
        AND1.setInputPin(0, OR1.getOut(0));
        AND1.setInputPin(1, NOT1.getOut(0));
        
        MEMACC.initVal(0);
        MEMACC.setInputPin(0, new Pin(0, 8));
        MEMACC.setPinLd(new Pin(false));
        MEMACC.setPinCL(Mem2.fcMEM());
        MEMACC.setPinInc(AND1.getOut(0));
        
        TIME.initVal(5);
        
        CMP1.setInputPin(0, MEMACC.getOut(0));
        CMP1.setInputPin(1, TIME.getOut(0));
        
        AND2.setInputPin(0, Mem1.rdMEM());
        AND2.setInputPin(1, Mem2.fcMEM());
        
        for (int i = 0; i < 8; i++) {
            zeroes.setInputPin(i, new Pin(false));
        }
        
    }
    
    public static void gui() {
        GuiLine line; // Pomocna promenljiva
        gui = new GuiScheme("src/images/Mem2.png");
        
        ArrayList<ArrayList<Point>> sections;
        ArrayList<Point> points;
        
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(94, 46));
        points.add(new Point(109, 46));
        sections.add(points);
        line = new GuiLine(sections, Mem1.rdMEM());
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(94, 57));
        points.add(new Point(109, 57));
        sections.add(points);
        line = new GuiLine(sections, Mem1.wrMEM());
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(130, 51));
        points.add(new Point(158, 51));
        sections.add(points);
        line = new GuiLine(sections, OR1.getOut(0));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(94, 76));
        points.add(new Point(256, 76));
        sections.add(points);
        points = new ArrayList<Point>();
        points.add(new Point(139, 76));
        points.add(new Point(139, 63));
        points.add(new Point(153, 63));
        sections.add(points);
        line = new GuiLine(sections, Mem2.fcMEM());
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(181, 57));
        points.add(new Point(256, 57));
        sections.add(points);
        line = new GuiLine(sections, AND1.getOut(0));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(415, 57));
        points.add(new Point(408, 57));
        sections.add(points);
        line = new GuiLine(sections, new Pin(false));
        gui.addLine(line);
        
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(332, 29));
        points.add(new Point(332, 48));
        sections.add(points);
        line = new GuiLine(sections, zeroes.getOut(0));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(408, 76));
        points.add(new Point(415, 76));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(332, 86));
        points.add(new Point(332, 228));
        points.add(new Point(453, 228));
        sections.add(points);
        line = new GuiLine(sections, MEMACC.getOut(0));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(604, 56));
        points.add(new Point(597, 56));
        sections.add(points);
        line = new GuiLine(sections, new Pin(true));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(525, 28));
        points.add(new Point(525, 47));
        sections.add(points);
        line = new GuiLine(sections, zeroes.getOut(0));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(604, 75));
        points.add(new Point(597, 75));
        sections.add(points);
        line = new GuiLine(sections, LogicalComponent.getCPUCLK());
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(525, 85));
        points.add(new Point(525, 133));
        points.add(new Point(402, 133));
        points.add(new Point(402, 192));
        points.add(new Point(453, 192));
        sections.add(points);
        line = new GuiLine(sections, TIME.getOut(0));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(536, 191));
        points.add(new Point(555, 191));
        sections.add(points);
        line = new GuiLine(sections, CMP1.getOut(1));
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(94, 136));
        points.add(new Point(105, 136));
        sections.add(points);
        line = new GuiLine(sections, Mem2.fcMEM());
        gui.addLine(line);
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(94, 148));
        points.add(new Point(105, 148));
        sections.add(points);
        line = new GuiLine(sections, Mem1.rdMEM());
        gui.addLine(line);
        
        sections = new ArrayList<ArrayList<Point>>();
        points = new ArrayList<Point>();
        points.add(new Point(128, 142));
        points.add(new Point(136, 142));
        sections.add(points);
        line = new GuiLine(sections, AND2.getOut(0));
        gui.addLine(line);

        // LABELE:
        gui.addLabel(new GuiLabel(420, 188, TIME.getOut(0)));
        gui.addLabel(new GuiLabel(420, 224, MEMACC.getOut(0)));
        
    }
    
    public static Pin fcMEM() {
        return CMP1.getOut(1);
    }
    
    public static Pin MEMout() {
        return AND2.getOut(0);
    }
}
